/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.menosehmais.controller.rest;

import br.com.caelum.vraptor.Get;
import java.util.Collection;
import br.com.caelum.vraptor.Delete;
import br.com.menosehmais.framework.loggin.StackTraceLogger;
import br.com.caelum.vraptor.Put;
import br.com.menosehmais.framework.persistence.Id;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;
import br.com.menosehmais.business.ServicoDeMarketingBO;
import br.com.menosehmais.dto.ServicoDeMarketingDTO;
import br.com.menosehmais.framework.persistence.DomainObject;
import br.com.menosehmais.framework.presentation.AbstractRestController;
import br.com.menosehmais.model.ServicoDeMarketingDO;
import javax.servlet.http.HttpServletRequest;

import static br.com.caelum.vraptor.view.Results.json;

/**
 * Controller para a secao de Servicos de Marketing
 * @author Junior JAFJ
 */
@Resource
public class ManterServicoMarketingController extends AbstractRestController {
    private final ServicoDeMarketingBO servicoBO;

    @Path("/manterServicoMarketing")
    public void indexManterServicoMarketing() {}

    public ManterServicoMarketingController(HttpServletRequest request, Result result, Validator validator,
            ServicoDeMarketingBO servicoBO) {
            super(request, result, validator);
            this.servicoBO = servicoBO;            
    }

    @Post
    @Path("/rest/servicoMarketing/")
    public void create(ServicoDeMarketingDTO servicoDTO) {
        validator.validate(servicoDTO);
        
        if (validator.hasErrors()) {
            validator.onErrorForwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
        }
        ServicoDeMarketingDO servicoDO = servicoDTO.toDomainObject();
        DomainObject domain = servicoBO.insert(servicoDO);
        if (domain != null) {
            result.use(json()).withoutRoot().from(domain).serialize();
        } else {
            result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
        }
    }

    @Put
    @Path("/rest/servicoMarketing/{id:" + Id.REGEX + "}")
    public void update(Id id, ServicoDeMarketingDTO servicoDTO) {
        servicoDTO.setId(id.toString());
        validator.validate(servicoDTO);
        if (validator.hasErrors()) {
            validator.onErrorForwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
        }
        ServicoDeMarketingDO servicoDO = servicoDTO.toDomainObject();
        DomainObject domain = servicoBO.update(servicoDO);
        if (domain != null) {
            result.use(json()).withoutRoot().from(domain).serialize();
        } else {
            result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
        }
    }

    @Delete
    @Path("/rest/servicoMarketing/{id:" + Id.REGEX + "}")
    public void destroy(Id id, ServicoDeMarketingDTO servicoDTO) {
        ServicoDeMarketingDO servicoDO = servicoBO.selectById(id.toString());
        if (servicoDO != null) {
            try {
                servicoBO.delete(servicoDO);
                result.use(json()).withoutRoot().from(true).serialize();
            } catch (Exception e) {
                StackTraceLogger.INSTANCE.printStackTrace(e);
                result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
            }
        } else {
            result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
        }
    }

    @Get
    @Path("/rest/servicoMarketing/{filtro}")
    public void show(String filtro, ServicoDeMarketingDTO servicoDTO) {
        servicoDTO = new ServicoDeMarketingDTO();
        servicoDTO.setFiltroNome(filtro);
        Collection<ServicoDeMarketingDO> servicoDO = servicoBO.listarServicosMarketingFiltrados(servicoDTO);
        if (servicoDO != null) {
            result.use(json()).withoutRoot().from(servicoDO).serialize();
        } else {
            result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
        }
    }
    
    @Post
    @Path("/rest/servicoMarketing/{id:" + Id.REGEX + "}")
    public void detail(Id id) {
        ServicoDeMarketingDO servicoDO = servicoBO.selectById(id.toString());
        if (servicoDO != null) {
            result.use(json()).withoutRoot().from(servicoDO).serialize();
        } else {
            result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
        }
    }
}
